//
//  KZReviewInformationView.m
//  lg_zhinengbeikao_english
//
//  Created by 朱凯 on 2019/1/7.
//  Copyright © 2019 zhukai. All rights reserved.
//

#import "KZReviewInformationView.h"

@interface KZDayView: UIView

- (void)setDay:(NSInteger)day animation:(BOOL)animation;

@end
@implementation KZDayView
{
    UILabel *_label;
}

- (instancetype)init {
    if (self = [super init]) {
        self.alpha = 1;
        self.backgroundColor = UIColor.whiteColor;
        self.layer.cornerRadius = 8;
        self.layer.masksToBounds = true;
        
        _label = [[UILabel alloc] init];
        _label.adjustsFontSizeToFitWidth = true;
        _label.numberOfLines = 1;
        _label.text = @"0";
        _label.font = B_FONT(W(35));
        _label.textAlignment = NSTextAlignmentCenter;
        _label.textColor = [UIColor blackColor];
        [self addSubview:_label];
        [_label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.width.height.equalTo(self);
            make.top.mas_equalTo(0);
        }];
    }
    return self;
}
- (void)setDay:(NSInteger)day animation:(BOOL)animation {
    
    UILabel *label = [[UILabel alloc] init];
    label.adjustsFontSizeToFitWidth = true;
    label.numberOfLines = 1;
    label.text = [NSString stringWithFormat:@"%li", day];
    label.font = B_FONT(W(35));
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor blackColor];
    [self addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.width.height.equalTo(self);
        make.top.mas_equalTo(-self.height);
    }];
    [self layoutIfNeeded];
    
    [UIView animateWithDuration:0.25 animations:^{
        [self->_label mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.height);
        }];
        [label mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
        }];
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        if (finished) {
            [self->_label removeFromSuperview];
            self->_label = label;
        }
    }];
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self setDay:arc4random() % 9 animation:true];
}

@end


@implementation KZReviewInformationView
{
    UIImageView *_topBgView;    // 顶部背景图
    UIImageView *_imageView1;   // 英语智能备考复习
    UIImageView *_imageView2;   // 距离中考还有
    UIImageView *_imageView3;   // 天
    
    KZDayView *_dayView1;
    KZDayView *_dayView2;
    KZDayView *_dayView3;
    
    
    UILabel *_dataLabel1;       // 备考累计
    UILabel *_dataLabel2;       // 完成试卷
    UILabel *_dataLabel2_2;     // 总共试卷
    UILabel *_dataLabel3;       // 平均分
    UILabel *_dataLabel4;       // 最高分
}

- (instancetype)init {
    if (self = [super init]) {
        
        self.backgroundColor = UIColor.whiteColor;
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    _topBgView = [UIImageView new];
    _topBgView.image = [UIImage imageNamed:@"kz-备考中心顶部背景"];
    [self addSubview:_topBgView];
    [_topBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.width.equalTo(self);
        make.height.mas_equalTo(H(220));
    }];
    
    _imageView1 = [UIImageView new];
    _imageView1.image = [UIImage imageNamed:@"kz-英语智能备考复习标题"];
    [self addSubview:_imageView1];
    [_imageView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(W(35));
        make.top.mas_equalTo(H(40));
    }];
    
    _imageView2 = [UIImageView new];
    _imageView2.image = [UIImage imageNamed:@"kz-距离中考还有"];
    [self addSubview:_imageView2];
    [_imageView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self->_imageView1);
        make.top.equalTo(self->_imageView1.mas_bottom).offset(H(25));
    }];
    
    _dayView2 = [KZDayView new];
    [self addSubview:_dayView2];
    [_dayView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self->_imageView2);
        make.height.mas_equalTo(H(50));
        make.width.mas_equalTo(W(32));
        make.top.equalTo(self->_imageView2.mas_bottom).offset(H(7));
    }];
    
    _dayView1 = [KZDayView new];
    [self addSubview:_dayView1];
    [_dayView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.top.equalTo(self->_dayView2);
        make.right.equalTo(self->_dayView2.mas_left).offset(W(-5));
    }];
    _dayView3 = [KZDayView new];
    [self addSubview:_dayView3];
    [_dayView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.top.equalTo(self->_dayView2);
        make.left.equalTo(self->_dayView2.mas_right).offset(W(5));
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [_dayView3 setDay:2 animation:true];
    });
    
    _imageView3 = [UIImageView new];
    _imageView3.image = [UIImage imageNamed:@"kz-天"];
    [self addSubview:_imageView3];
    [_imageView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self->_dayView3.mas_right).offset(W(10));
        make.bottom.equalTo(self->_dayView3).offset(H(-10));
    }];
    
    NSArray *titles = @[@"备考累计", @"完成试卷", @"平均分", @"最高分"];
    NSArray *units = @[@"天", @"份", @"分", @"分"];
    NSArray *colors = @[kOrangeColor, kBlueColor, kGreenColor, kRedColor];
    UIView *last = nil;
    for (int i = 0; i < titles.count; i ++) {
        UIView *view = [UIView new];
        [self addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(self).multipliedBy(0.25);
            make.top.equalTo(self->_topBgView.mas_bottom);
            make.bottom.mas_equalTo(0);
            if (last) {
                make.left.equalTo(last.mas_right);
            } else {
                make.left.mas_equalTo(0);
            }
        }];
        last = view;
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.font = FONT(14);
        titleLabel.text = titles[i];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.textColor = UIColor.lightGrayColor;
        [view addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.mas_equalTo(0);
        }];
        
        UILabel *dataLabel = [[UILabel alloc] init];
        dataLabel.text = @"24";
        dataLabel.font = B_FONT(32);
        dataLabel.textAlignment = NSTextAlignmentCenter;
        dataLabel.textColor = colors[i];
        [view addSubview:dataLabel];
        [dataLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(titleLabel.mas_top).offset(H(0));
            make.centerX.equalTo(view);
        }];
        
        UILabel *unitLabel = [[UILabel alloc] init];
        unitLabel.text = units[i];
        unitLabel.font = B_FONT(12);
        unitLabel.textAlignment = NSTextAlignmentCenter;
        unitLabel.textColor = UIColor.blackColor;
        [view addSubview:unitLabel];
        [unitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(dataLabel.mas_right).offset(W(2));
            make.centerY.equalTo(dataLabel);
        }];
        
        switch (i) {
            case 0:
                _dataLabel1 = dataLabel;
                break;
            case 1:
            {
                _dataLabel2 = dataLabel;
                [_dataLabel2 mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.right.equalTo(view.mas_centerX);
                    make.bottom.equalTo(titleLabel.mas_top).offset(H(0));
                }];
                _dataLabel2_2 = [[UILabel alloc] init];
                _dataLabel2_2.text = @"/81";
                _dataLabel2_2.font = B_FONT(12);
                _dataLabel2_2.textAlignment = NSTextAlignmentCenter;
                _dataLabel2_2.textColor = UIColor.blackColor;
                [view addSubview:_dataLabel2_2];
                [_dataLabel2_2 mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(view.mas_centerX);
                    make.centerY.equalTo(self->_dataLabel2);
                }];
                [unitLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(self->_dataLabel2_2.mas_right).offset(W(5));
                    make.centerY.equalTo(self->_dataLabel2_2);
                }];
            }
                break;
            case 2:
                _dataLabel3 = dataLabel;
                break;
            case 3:
                _dataLabel4 = dataLabel;
                break;
            default:
                break;
        }
    }
}


@end
